import request from '../../util/request';

let loginTask = null;

const state = () => {
  const 城市 = {
    苏州 : {
      centerPoint : [120.71518979680582, 31.36062650725843],
      shape : [
        [
          [113, 36],
          [130, 36],
          [130, 25],
          [113, 25],
          [113, 36]
        ]
      ],
      bounds : [
        [114.9501630401628, 26.908712759288534],
        [128.66557242808227, 35.72484441155501],
      ],
      districtKey : '320500',
      districtName : '苏州'
    },
    常州 : {
      // centerPoint : [119.88458353183614, 31.554035371006478],
      centerPoint: [119.90792947910995, 31.627731717919943],
      shape : [
        [
          [114, 40],
          [127, 40],
          [127, 28],
          [114, 28],
          [114, 40]
        ]
      ],
      bounds : [
        [115.60569996444406, 29.291164221662754],
        [124.32423788614295, 34.59706854808928],
      ],
      districtKey : '320400',
      districtName: '常州'
    },
    淮安 : {
      centerPoint: [119.11682979006,33.548319190619],
      districtKey : '320800',
      districtName: '淮安'
    }
  }

  const currentCity = 城市.淮安;

  // var funcConnect = (function(){
  //   function funcConnect() {}
  //
  //   funcConnect.prototype._connect = function(obj, type, callback) {
  //     var _func = obj[type];
  //     obj[type] = function() {
  //       var _args = arguments;
  //       _func.apply(obj, _args);
  //       callback.apply(obj, _args);
  //     };
  //   };
  //
  //   return funcConnect;
  // })();
  //
  // let tokenTask = SGMap.tokenTask;
  //
  // var fc = new funcConnect();
  // fc._connect(tokenTask,"_setToken",function(accessToken, refreshToken){
  //   request({
  //     url: '/mapAuth/updateToken',
  //     method: 'post',
  //     data : {
  //       'token' : accessToken
  //     }
  //   });
  // });

  return {
    city : currentCity,
    epgis : epgis,
    isLogin : false
  }
}

const mutations = {
  LOGIN_IN : (state) => {
    state.isLogin = true;
  }
}

const actions = {
  // map login
  login({commit}, mapInfo) {
    const {key, secret} = mapInfo;
    if(loginTask) return loginTask;
    loginTask = new Promise((resolve, reject) => {
      try {
        epgis.tokenTask.login(key, secret).then(() => {
          commit('LOGIN_IN');
          resolve();
            // SGMap.plugin(["SGMap.RoadNetLayer"]).then(function (res) {
            //     resolve();
            // });
          // SGMap.plugin("SGMap.AutoCompletePlusTask").then(function (res) {
          //   resolve(res);
          // });
        });
      } catch (e) {
        reject(e);
      }
    });
    return loginTask;
  },
}
export default {
  namespaced : true,
  state,
  mutations,
  actions
}
